/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package peticionweb.control;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import peticionweb.logica.entidad.Cupar1a;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import peticionweb.control.exceptions.IllegalOrphanException;
import peticionweb.control.exceptions.NonexistentEntityException;
import peticionweb.logica.entidad.Cucar1m;

/**
 *
 * @author icardenas
 */
public class Cucar1mJpaController implements Serializable {

    public Cucar1mJpaController() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("PeticionWebPU");
        this.emf = factory;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Cucar1m cucar1m) {
        if (cucar1m.getCupar1aList() == null) {
            cucar1m.setCupar1aList(new ArrayList<Cupar1a>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Cupar1a> attachedCupar1aList = new ArrayList<Cupar1a>();
            for (Cupar1a cupar1aListCupar1aToAttach : cucar1m.getCupar1aList()) {
                cupar1aListCupar1aToAttach = em.getReference(cupar1aListCupar1aToAttach.getClass(), cupar1aListCupar1aToAttach.getCupar1aPK());
                attachedCupar1aList.add(cupar1aListCupar1aToAttach);
            }
            cucar1m.setCupar1aList(attachedCupar1aList);
            em.persist(cucar1m);
            for (Cupar1a cupar1aListCupar1a : cucar1m.getCupar1aList()) {
                Cucar1m oldCucar1mOfCupar1aListCupar1a = cupar1aListCupar1a.getCucar1m();
                cupar1aListCupar1a.setCucar1m(cucar1m);
                cupar1aListCupar1a = em.merge(cupar1aListCupar1a);
                if (oldCucar1mOfCupar1aListCupar1a != null) {
                    oldCucar1mOfCupar1aListCupar1a.getCupar1aList().remove(cupar1aListCupar1a);
                    oldCucar1mOfCupar1aListCupar1a = em.merge(oldCucar1mOfCupar1aListCupar1a);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Cucar1m cucar1m) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cucar1m persistentCucar1m = em.find(Cucar1m.class, cucar1m.getCodca());
            List<Cupar1a> cupar1aListOld = persistentCucar1m.getCupar1aList();
            List<Cupar1a> cupar1aListNew = cucar1m.getCupar1aList();
            List<String> illegalOrphanMessages = null;
            for (Cupar1a cupar1aListOldCupar1a : cupar1aListOld) {
                if (!cupar1aListNew.contains(cupar1aListOldCupar1a)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Cupar1a " + cupar1aListOldCupar1a + " since its cucar1m field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Cupar1a> attachedCupar1aListNew = new ArrayList<Cupar1a>();
            for (Cupar1a cupar1aListNewCupar1aToAttach : cupar1aListNew) {
                cupar1aListNewCupar1aToAttach = em.getReference(cupar1aListNewCupar1aToAttach.getClass(), cupar1aListNewCupar1aToAttach.getCupar1aPK());
                attachedCupar1aListNew.add(cupar1aListNewCupar1aToAttach);
            }
            cupar1aListNew = attachedCupar1aListNew;
            cucar1m.setCupar1aList(cupar1aListNew);
            cucar1m = em.merge(cucar1m);
            for (Cupar1a cupar1aListNewCupar1a : cupar1aListNew) {
                if (!cupar1aListOld.contains(cupar1aListNewCupar1a)) {
                    Cucar1m oldCucar1mOfCupar1aListNewCupar1a = cupar1aListNewCupar1a.getCucar1m();
                    cupar1aListNewCupar1a.setCucar1m(cucar1m);
                    cupar1aListNewCupar1a = em.merge(cupar1aListNewCupar1a);
                    if (oldCucar1mOfCupar1aListNewCupar1a != null && !oldCucar1mOfCupar1aListNewCupar1a.equals(cucar1m)) {
                        oldCucar1mOfCupar1aListNewCupar1a.getCupar1aList().remove(cupar1aListNewCupar1a);
                        oldCucar1mOfCupar1aListNewCupar1a = em.merge(oldCucar1mOfCupar1aListNewCupar1a);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cucar1m.getCodca();
                if (findCucar1m(id) == null) {
                    throw new NonexistentEntityException("The cucar1m with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Cucar1m cucar1m;
            try {
                cucar1m = em.getReference(Cucar1m.class, id);
                cucar1m.getCodca();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cucar1m with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Cupar1a> cupar1aListOrphanCheck = cucar1m.getCupar1aList();
            for (Cupar1a cupar1aListOrphanCheckCupar1a : cupar1aListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Cucar1m (" + cucar1m + ") cannot be destroyed since the Cupar1a " + cupar1aListOrphanCheckCupar1a + " in its cupar1aList field has a non-nullable cucar1m field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(cucar1m);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Cucar1m> findCucar1mEntities() {
        return findCucar1mEntities(true, -1, -1);
    }

    public List<Cucar1m> findCucar1mEntities(int maxResults, int firstResult) {
        return findCucar1mEntities(false, maxResults, firstResult);
    }

    private List<Cucar1m> findCucar1mEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Cucar1m.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Cucar1m findCucar1m(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Cucar1m.class, id);
        } finally {
            em.close();
        }
    }

    public int getCucar1mCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Cucar1m> rt = cq.from(Cucar1m.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
